import 'dart:io';

import 'package:FineArt/components/horizon_scroll.dart';
import 'package:FineArt/components/rounded_text.dart';
import 'package:FineArt/constants.dart';
import 'package:FineArt/core/api/material_api.dart';
import 'package:FineArt/core/api/module_api.dart';
import 'package:FineArt/core/api/resource_api.dart';
import 'package:FineArt/core/utils/toast.dart';
import 'package:FineArt/core/widget/grid/grid_item.dart';
import 'package:FineArt/core/widget/grid/grid_item_photo.dart';
import 'package:FineArt/core/widget/grid/grid_item_teach.dart';
import 'package:FineArt/core/widget/list/article_item.dart';
import 'package:FineArt/page/tab/MyMaterialFolder.dart';
import 'package:FineArt/page/tab/recommend/component/recommendItem.dart';
import 'package:FineArt/utils/log_util.dart';
import 'package:FineArt/utils/size_config.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class CreateFolderPage extends StatefulWidget {
  int pId;
  FolderBean updateFolder;
  static bool isCreateFolder = false;

  CreateFolderPage({Key key, this.pId, this.updateFolder});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    LogUtil.d("CreateFolderPage _pid:$pId");
    return new _CreateFolderPagePageState();
  }
}

class _CreateFolderPagePageState extends State<CreateFolderPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    var width = MediaQuery.of(context).size.width * 0.76;

    return Scaffold(
      backgroundColor: Color.fromRGBO(0xff, 0xff, 0xff, 0.26),
      body: Padding(
          padding: EdgeInsets.symmetric(
              vertical: 0, horizontal: getPortraitSWidth(16)),
          child: Column(
            children: [
              SizedBox(
                height: getPortraitSHeight(30),
              ),
              Padding(
                padding: EdgeInsets.symmetric(vertical: 2, horizontal: 2),
                child: Row(
                  children: [
                    InkWell(
                      child: Image.asset(
                        "assets/images/icon_folder_close.png",
                        width: getPortraitSWidth(30),
                        height: getPortraitSHeight(30),
                      ),
                      onTap: () {
                        Navigator.of(context).pop();
                      },
                    ),
                    Expanded(
                      child: Text(""),
                    ),
                    InkWell(
                      child: Text(
                        "确定",
                        style:
                            TextStyle(color: Colors.white, fontSize: setSp(15)),
                      ),
                      onTap: () {
                        LogUtil.d(" 确定 onclick");
                        // this.startFavorite();
                        LogUtil.d("$folderName");
                        if (widget.updateFolder != null) {
                          modifyAction();
                        } else
                          createAction();
                      },
                    )
                  ],
                ),
              ),
              Expanded(child: getFolderItem())
            ],
          )),
    );
  }

  void createAction() {
    if (folderName == null || folderName.isEmpty) {
      ToastUtils.toast("文件夹名称不能为空");
      return;
    }
    LogUtil.d("pid ${widget.pId}");
    DirRequestBody body = DirRequestBody(
        pId: widget.pId, label: folderName, typeGroup: "PRIVATE");
    MaterialApi().createDir(body).then((result) {
      if (result.code == 200) {
        CreateFolderPage.isCreateFolder = true;
        Navigator.of(context).pop();
      } else {
        ToastUtils.toast("创建失败 ${result.msg}");
      }
    });
  }

  void modifyAction() {
    if (folderName == null || folderName.isEmpty) {
      ToastUtils.toast("文件夹名称不能为空");
      return;
    }
    LogUtil.d("pid ${widget.pId}");
    int id = int.parse(widget.updateFolder.id);
    DirRequestBody body = DirRequestBody(
        pId: widget.pId,
        label: folderName,
        typeGroup: "PRIVATE",
        id: id);
    MaterialApi().modifyDir(id,body).then((result) {
      if (result.code == 200) {
        CreateFolderPage.isCreateFolder = true;
        ToastUtils.toast("${result.msg}");
        Navigator.of(context).pop();
      } else {
        ToastUtils.toast("修改失败 ${result.msg}");
      }
    });
  }

  String folderAssetsUrl = "assets/images/icon_folder.png";
  String folderName;

  Widget getFolderItem() {
    return Center(
        child: Container(
      child: Stack(alignment: Alignment.center, children: [
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Image.asset(
              folderAssetsUrl,
              width: getPortraitSWidth(80),
            ),
            getField()
          ],
        ),
      ]),
    ));
  }

  Widget getField() {
    TextEditingController _controller = new TextEditingController();
    if (widget.updateFolder != null) {
      _controller.text = widget.updateFolder.name;
      folderName = widget.updateFolder.name;
    }
    return Theme(
      data: new ThemeData(primaryColor: Colors.white, hintColor: Colors.white),
      child: TextField(
        controller: _controller,
        maxLength: 30,
        //最大长度，设置此项会让TextField右下角有一个输入数量的统计字符串
        maxLines: 1,
        //最大行数
        autocorrect: true,
        //是否自动更正
        autofocus: true,
        //是否自动对焦
        obscureText: false,
        //是否是密码
        textAlign: TextAlign.center,
        //文本对齐方式
        style: TextStyle(fontSize: 16.0, color: Colors.white),
        //输入文本的样式
        onChanged: (text) {
          //内容改变的回调
          print('folderName $text');
          folderName = text;
        },
        decoration: InputDecoration(
            hintText: '请输入',
            hintStyle: TextStyle(fontSize: 14, color: Colors.white)),
      ),
    );
  }
}

// class FolderBean {
//   String imageUrl;
//   String name;
//   String id;
//   bool isCheck = false;
//
//   FolderBean(this.imageUrl, this.name, this.id);
// }
